package yxy.game.pm2.command.hitplanegame;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.planefight;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_国庆寻宝;
import yxy.game.pm2.active.module.ActiveModule_心愿瓶;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HitPlaneMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hitplanegame.HitPlaneGameModule;

import java.util.ArrayList;
import java.util.Collection;


/****************************************
 * @name CMD_HitPlaneReward
 * @description 打飞机过关的奖励领取
 * @author 梁健彬
 * @since 2022-09-05
 ***************************************/
@MessageType(proto = Command.HitPlaneReward, classtype = HitPlaneMessage.HitPlaneRewardRequest.class)
public class CMD_HitPlaneReward extends Command<HitPlaneMessage.HitPlaneRewardRequest> {

    @Override
    public void execute(MessageSource messageSource, HitPlaneMessage.HitPlaneRewardRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int id = request.getId();
        HitPlaneMessage.HitPlaneRewardResponse.Builder response = HitPlaneMessage.HitPlaneRewardResponse.newBuilder();
        try {

            HitPlaneGameModule module = HitPlaneGameModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            planefight data = DataCenter.getData(id, planefight.class);
            if (data == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            if (module.getCurrentId() < id) {
                send(messageSource, response, Result.DataError);
                return;
            }

            ArrayList<Integer> rewardList = module.getRewardList();
            if (rewardList.contains(id)) {
                send(messageSource, response, Result.DataError);
                return;
            }

            rewardList.add(id);
            CurrencyHandle currencyHandle = new CurrencyHandle();
            PlayerBean pb = PlayerBean.get(playerCode);
            ServerBean sb = ServerBean.get(pb.getServerId());
            activeControl sai_心愿瓶 = activeControl.get(String.valueOf(sb.getAreaId()), String.valueOf(ActiveModule_心愿瓶.acttype));
            activeControl sai_国庆寻宝 = activeControl.get(String.valueOf(sb.getAreaId()), String.valueOf(ActiveModule_国庆寻宝.acttype));
            if (sai_心愿瓶.isRuning()) {
                Currencies curs = new Currencies(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));
                Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
                currencyHandle.addCurrency(playerCode, CCReason.打飞机玩法, curs);
                response.addAllReward(items);
                module.set();

                send(messageSource, response, Result.Success);
            } else if (sai_国庆寻宝.isRuning()) {
                Currencies curs = new Currencies(Currency.create(data.getRewardtype2(), data.getRewardid2(), data.getRewardnum2()));
                Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
                currencyHandle.addCurrency(playerCode, CCReason.打飞机玩法, curs);
                response.addAllReward(items);
                module.set();

                send(messageSource, response, Result.Success);
            } else {
                send(messageSource, response, Result.DataError);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, HitPlaneMessage.HitPlaneRewardResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
